<?

/**
* Global Handler functions
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @package CGHandlers
*
*/

class CGHandlers{

  /**
   * insert into Mutation Invoice
   *
   * @param Database  $Db
   * @param string    $whose
   * @param string    $values
   * @param string    $srcDb
   */
  function insToMutInvoice(&$Db,
                            $whose,
                           &$values,
                            $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (PERIODE,
                ".$whose.",
                REFERENCE,
                SOURCE,
                DATE_SOURCE,
                TR_DATE,
                DUE_DATE,
                TX_TYPE,
                AMOUNT,
                NO)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
  * insert into model input history
  *
  * @param  string  $Db
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insTxHistory(&$Db,
                        &$values,
                         $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                TR_DATE,
                MODEL_ID,
                QTY,
                PRICE,
                NET,
                NO,
                TX_TYPE)
               VALUES (".$values.") ";

    $Db->execQuery($query);
  }

  /**
   * insert into SN detail
   *
   * @param Database  $Db
   * @param string    $values
   * @param string    $srcDb
   */
  function insToSnDtl(&$Db,
                      &$values,
                       $srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (JOB_ID,
                NO,
                MODEL_ID,
                QTY,
                HEAD,
                LENGTH,
                SER_NUM)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
   * update SN's warehouse
   *
   * @param Database  $Db
   * @param string    $SN
   * @param string    $updateTo
   * @param Composer  $SourceDb
   */
  function updSnWhsBySN(&$Db,
                        &$SN,
                        &$updateTo,
                        &$SourceDb){

    /**
     * Check out if SN exist
     */
    CGFetchers::fetchIsSnExists($Db,
                                $SN,
                                $SourceDb->fetchValue("srcDbSN"),
                                $isExist);

    if($isExist){

      /* Do nothing */
    }else{

      Misc::usrDieJs("Error: SN is not exists");
    }

    $query  = "UPDATE ".$SourceDb->fetchValue("srcDbSN")."
               SET WAREHOUSE='".$updateTo."'
               WHERE  SN='".$SN."' ";

    $Db->execQuery($query);

  }

  /**
   * update SN's warehouse
   *
   * @param Database  $Db
   * @param integer   $modelId
   * @param string    $updateTo
   * @param string    $notaSrc
   * @param Composer  $SourceDb
   */
  function updSnWhs(&$Db,
                    &$modelId,
                    &$updateTo,
                    &$notaSrc,
                    &$SourceDb){

    $query	= "SELECT H.JOB_ID,
                      D.NO
               FROM ".$SourceDb->fetchValue("srcDbSnHdr")." H,
                    ".$SourceDb->fetchValue("srcDbSnDtl")." D
               WHERE	H.NOTA_SRC='".$notaSrc."'
                      AND D.JOB_ID=H.JOB_ID
                      AND D.MODEL_ID='".$modelId."' ";

    $Db->execFetchQuery($query,
                        $nrows,
                        $result);

    if($nrows > 0){

      $query  = "UPDATE ".$SourceDb->fetchValue("srcDbSnSubDtl")."
                 SET WAREHOUSE='".$updateTo."'
                 WHERE  JOB_ID='".$result["JOB_ID"][0]."'
                        AND DTL_NO=".$result["NO"][0]." ";

      $Db->execQuery($query);
    }else{

      /**
       * Since this modelId is not SN-ed
       * then skip
       */
    }
  }

  /**
   * insert into warehouse balance
   *
   * @param Database  $Db
   * @param integer   $modelId
   * @param string    $unit
   * @param integer   $qty
   * @param string    $srcDb
   */
  function insToWhsBalance(&$Db,
                            $modelId,
                            $unit,
                            $qty,
                            $srcDb){

    CGFetchers::fetchWhsBalance($Db,
                                $modelId,
                                $unit,
                                $srcDb,
                                $nrows,
                                $curBalance);

    $newBalance = $curBalance + $qty;

    if($nrows > 0){

      $query  = "UPDATE ".$srcDb."
                 SET QTY=".$newBalance."
                 WHERE  MODEL_ID='".$modelId."' ";
    }else{

      CGFetchers::fetchLstNumberWhs($Db,
                                    $modelId,
                                    $unit,
                                    $srcDb,
                                    $lstNo);

      $values = "  ".$lstNo." ,     ".
                " '".$modelId."',   ".
                " '".$unit."',      ".
                "  ".$newBalance."  ";

      $query  = "INSERT INTO ".$srcDb."
                 (NO,
                  MODEL_ID,
                  UNIT,
                  QTY)
                 VALUES (".$values.") ";

    }


    $Db->execQuery($query);
  }

  /**
   * insert into SN
   *
   * @param Database  $Db
   * @param string    $values
   * @param String    $srcDb
   */
  function insToSN($Db,
                   $values,
                   $srcDb){

    $query	= "INSERT INTO ".$srcDb."
               (JOB_ID,
                DTL_NO,
                NO,
                SN,
                WAREHOUSE,
                TX_TYPE)
               VALUES (".$values.")";

    $Db->execQuery($query);
  }

  /**
  * init the serialization values
  *
  * @param  string  $COracle
  * @param  string  $userId
  * @param  string  $srcValues
  * @param  string  $srcDbHdr
  * @param  string  $srcDbDtl
  *
  */
  function initSerialize(&$COracle,
                         &$userId,
                         &$srcValues,
                         &$srcDbHdr,
                         &$srcDbDtl){


  }

  /**
  * insert to db log
  *
  * @param  string  $COracle
  * @param  string  $today_date
  * @param  string  $jobId
  * @param  string  $userIp
  * @param  string  $operation
  * @param  string  $srcDb
  *
  */
  function insToDbLog(&$COracle,
                      &$today_date,
                      &$jobId,
                      &$userId,
                      $userIp,
                      $operation,
                      &$srcDb){

    CGFetchers::fetchLastNoByDate($COracle,
                                  $today_date,
                                  $srcDb,
                                  $lastNo);

    $logDate = "TO_DATE('".$today_date."', 'dd-mm-YYYY')";

    $values = "  ".$lastNo." , ".
              " '".$jobId."', ".
              " '".$userId."', ".
              "  ".$operation." , ".
              " '".$userIp."', ".
              "  ".$logDate."  ";

    $query  = "INSERT INTO ".$srcDb."
               (NO,
                JOB_ID,
                ENTRIED_BY,
                OPERATION,
                IP,
                TR_DATE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * update the current last no of sequence number
  * if the update is failed then it returns to $seqEditStatus
  * where true  = success,
  *       false = fail.
  *
  * @param  string  $COracle
  * @param  string  $thisSequence
  * @param  string  $srcJidIndex
  * @param  string  $thisPeriode
  * @param  string  $srcDb
  * @param  string  $seqEditStatus
  *
  */
  function initCurSequence(&$COracle,
            	             &$thisSequence,
            	             &$srcJidIndex,
            	             &$thisPeriode,
            	             &$srcDb,
            	             &$seqEditStatus){

    $srcPeriode = "TO_DATE('".$thisPeriode."','mm-YYYY')";

    $thisIndex  = $srcJidIndex + 1;

    /**
    * SEQ_NUMBER is included because
    * we need consistency
    *
    */
    $query  = "UPDATE ".$srcDb."
               SET SEQ_NUMBER=".$thisIndex."
               WHERE  SEQ_NAME='".$thisSequence."'
                      AND PERIODE=".$srcPeriode."
                      AND SEQ_NUMBER=".$srcJidIndex." ";

    $seqEditStatus = $COracle->execQuery($query);
  }

  /**
  * Delete records by condition with thisId
  *
  * @param  Database $Db
  * @param  string   $condt
  * @param  string   $thisId
  * @param  string   $srcDb
  *
  */
  function dbEntDelete2(&$Db,
                         $condt,
                         $thisId,
                        &$srcDb){

    $query  = "DELETE ".$srcDb."
               WHERE  ".$condt."='".$thisId."' ";

    $Db->execQuery($query);
  }

  /**
  * Delete records by jobId
  *
  * @param  string  $COracle
  * @param  string  $jobId
  * @param  string  $srcDb
  *
  */
  function dbEntDelete(&$COracle,
                       &$jobId,
                        $srcDb){

    $query  = "DELETE ".$srcDb."
               WHERE  JOB_ID='".$jobId."' ";

    $COracle->execQuery($query);
  }

  /**
  * process the approval
  *
  * @param  string  $COracle
  * @param  string  $apprThisJobId
  * @param  string  $refId
  * @param  string  $userId
  * @param  string  $srcDb
  *
  */
  function procApproval(&$COracle,
                        &$apprThisJobId,
                        &$refId,
                        &$userId,
                        &$srcDb){

    $today  = " TO_DATE('".date("d-m-Y")."','dd-mm-YYYY') ";

    $query  = "UPDATE ".$srcDb."
               SET REFERENCE='".$refId."',
                   APPROVED='".$userId."',
                   APPROVED_DATE=".$today.",
                   STATUS='"._ACTIVE."'
               WHERE  JOB_ID='".$apprThisJobId."' ";

    $COracle->execQuery($query);
  }

  /**
  * process the cancelation
  *
  * @param  string  $COracle
  * @param  string  $cclThisJobId
  * @param  string  $userId
  * @param  string  $srcDb
  *
  */
  function procCancel(&$COracle,
                      &$cclThisJobId,
                      &$userId,
                      &$srcDb){

    $today  = " TO_DATE('".date("d-m-Y")."','dd-mm-YYYY') ";

    $query  = "UPDATE ".$srcDb."
               SET STATUS='"._CANCELED."',
                   CANCELED='".$userId."',
                   CANCELED_DATE=".$today."
               WHERE  JOB_ID='".$cclThisJobId."' ";

    $COracle->execQuery($query);
  }

  /**
  * process the sequence reentry
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insSeqEntry( &$COracle,
                        &$values,
                        &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (PERIODE,
                SEQ_NAME,
                SEQ_NUMBER)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert the backup sequence detail
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insBoSeqDtl( &$COracle,
                        &$values,
                        &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (DATA_ID,
                PERIODE,
                SEQ_NAME,
                SEQ_NUMBER)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * insert the backup sequence header
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  string  $srcDb
  *
  */
  function insBoSeqHdr(&$COracle,
                       &$values,
                       &$srcDb){

    $query  = "INSERT INTO ".$srcDb."
               (DATA_ID,
                TR_DATE,
                NO)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }

  /**
  * update the master sequence
  *
  * @param  string  $COracle
  * @param  integer $index
  * @param  Array   $entSequence
  * @param  string  $srcDb
  *
  */
  function updateMstSequence( &$COracle,
                              &$index,
                              &$entSequence,
                              &$srcDb){

    /**
    * We need to check if the seq name with
    * the same periode is exist
    * YES: update
    * NO : insert
    *
    */
    CGFetchers::fetchIsSeqExists($COracle,
                                 $index,
                                 $entSequence,
                                 $srcDb,
                                 $isExists);

    $strDate = date("d-m-Y",
                    strtotime($entSequence["PERIODE"][$index]));

    $periode = " TO_DATE('".$strDate."', 'dd-mm-YYYY') ";

    if($isExists){

      $query  = "UPDATE ".$srcDb."
                 SET SEQ_NUMBER=".$entSequence["SEQ_NUMBER"][$index]."
                 WHERE  SEQ_NAME='".$entSequence["SEQ_NAME"][$index]."'
                        AND PERIODE=".$periode." ";

      $COracle->execQuery($query);

    }else{

      $values = "  ".$periode." , ".
                " '".$entSequence["SEQ_NAME"][$index]."', ".
                "  ".$entSequence["SEQ_NUMBER"][$index]."   ";

      $query  = "INSERT INTO ".$srcDb."
                 (PERIODE,
                  SEQ_NAME,
                  SEQ_NUMBER)
                 VALUES (".$values.") ";

      $COracle->execQuery($query);
    }
  }

  /**
  * insert into this model name and model id history
  *
  * @param  string  $COracle
  * @param  string  $values
  * @param  integer $txType
  * @param  string  $srcDb
  *
  */
  function insToHistory( &$COracle,
                         &$values,
                         &$txType,
                         &$srcDb){

    $values .= ",".$txType." ";

    $query  = "INSERT INTO ".$srcDb."
               (NO,
                TR_DATE,
                SRC_REF,
                SRC_NO,
                MODEL,
                MODEL_ID,
                UNIT,
                SER_NUMBER,
                TX_TYPE)
               VALUES (".$values.") ";

    $COracle->execQuery($query);
  }
}
?>